package com.financing.wap.service.weixin.config;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ResourceBundle;

/**
 * 微信公众号主配置
 * <p>
 * Created by bjliumingbo on 2017/5/12.
 *
 * @author FirenzesEagle
 * @author BinaryWang
 */
public final class WxMpJincsConfig {
	private static final Logger logger = LoggerFactory.getLogger(WxMpJincsConfig.class);

	private static WxMpService wxMpService ;
	private static WxMpConfigStorage wxMpConfigStorage;

	static{
		init();
	}

    private WxMpJincsConfig() {
    }

    /**
	 * 初始化微信配置及服务
	 * <p>wx.properties</p>
	 */
	private static void init() {
		ResourceBundle resource = ResourceBundle.getBundle("wx");
		String appid = resource.getString("wx.jincs.appid");
		String appsecret = resource.getString("wx.jincs.appsecret");
		String token = resource.getString("wx.jincs.token");
		String aeskey = resource.getString("wx.jincs.aeskey");
		String templateId = resource.getString("wx.jincs.templateId");

		if (logger.isInfoEnabled()) {
            logger.info("appid:{0}, appsecret:{1}, token:{2}, aeskey:{3}.", appid, appsecret, token, aeskey);
        }
		WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
		configStorage.setAppId(appid);
		configStorage.setSecret(appsecret);
		configStorage.setToken(token);
		configStorage.setAesKey(aeskey);
		configStorage.setTemplateId(templateId);
		wxMpConfigStorage = configStorage;
        wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
	}

	public static WxMpService getWxMpService() {
		return wxMpService;
	}

	public static WxMpConfigStorage getWxMpConfigStorage() {
		return wxMpConfigStorage;
	}
}
